<?php
Class TreetraverseComponent extends Object {
	
	var $nodeList;
	var $tree;
	var $traversing_for = null;
	var $locations = null;

	function array_on_id($unsortedArray) {
		foreach($unsortedArray as $records) {
			$this->nodeList[$records[$this->traversing_for]['id']] = $records;
		}
	}
	
	function treeDepth($arr_tree, $modelName, $parent_id) {
		$this->traversing_for = $modelName;
		$this->array_on_id($arr_tree);
		$this->traverse();	
	}	
	
	function traverse() {
		foreach($this->nodeList as $index => $records) {
			if($records[$this->traversing_for]['parent_id'] != null) {
				$location = $this->setlocation($records[$this->traversing_for]['id'], $records[$this->traversing_for]['parent_id']);
				unset($this->nodeList[$index]);
			}
		}
		foreach($this->nodeList as $index => $records) {
			if($records[$this->traversing_for]['parent_id'] != null) {
				$this->traverse();
			}
		}
		return;
		
	}

	function setlocation($id, $parent) {
		if(!is_array($this->locations) || !in_array($id, $this->locations)) {
			$this->location[$id] = '['. $parent. ']';
			return;
		}
		$this->location[$id] = '['. $this->location[$id]. ']'. $this->location[$parent];
	}
}		
/*
		$this->tree = null;
		foreach($this->nodeList as $index => $records ) {
			if($records[$modelName][$parent_id] == null) {
				$this->tree['parent'][$records[$modelName]['id']] = $records;	
				unset($this->nodeList[$index]);
				$this->tree['parent']
					 [$records[$modelName]['id']]
					 ['childs'] = $this->associate_childs($arr_tree, $records[$modelName]['id']);
			}
		}
		
		debug($this->tree);
	}
	
	function associate_childs($id) {
		foreach($this->nodeList as $index => $record) {
			if($records[$modelName][$parent_id] == $id) {
				$childs[] = $records[$modelName][$parent_id];
				unset($this->nodeList[$index]);
			}
		}
		return $childs;
	}
}
*/